package cn.org.guhao.practice.go.security.util;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * EncryptUtil
 *
 * @author guhao
 * @since 2020/7/18
 */
public class EncryptUtil {

  public static String md5(String plainText) {
    byte[] secretBytes = null;
    try {
      secretBytes = MessageDigest.getInstance("md5").digest(
          plainText.getBytes());
    } catch (NoSuchAlgorithmException e) {
      throw new RuntimeException("没有这个md5算法！");
    }
    StringBuilder md5code = new StringBuilder(new BigInteger(1, secretBytes).toString(16));
    for (int i = 0; i < 32 - md5code.length(); i++) {
      md5code.insert(0,"0");
    }
    return md5code.toString();
  }

}
